package utils

import (
	"plugin"
	"runtime"
	"strings"

	"gitee.com/xuesongtao/gotool/xdebug"
	"gitee.com/xuesongtao/gotool/xlog"
)

type enc2decFn = func(in string) (out string)

var (
	Dec enc2decFn // 解密
)

func InitDecFn(projectDir string) {
	split := xdebug.GetSysPathSplitSymbol()
	soFileName := ""
	switch runtime.GOOS {
	case "linux":
		soFileName = "enc2dec_linux.so"
	case "darwin":
		soFileName = "enc2dec_darwin.so"
	default:
		Dec = func(in string) (out string) {
			return
		}
		return
	}

	soPath := strings.Join([]string{projectDir, "utils", soFileName}, split)
	f, err := plugin.Open(soPath)
	if err != nil {
		xlog.Panic("plugin.Open is failed, err: ", err)
	}

	dec, err := f.Lookup("MyDec")
	if err != nil {
		xlog.Panic("f.Lookup(\"MyDec\") is failed, err: ", err)
	}
	Dec = dec.(enc2decFn)
}
